<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>orderPreview</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <link rel="stylesheet" th:href="@{/bootstrap.min.css}" />
    <script th:src="@{/vue.js}"></script>
</head>
<body>

<div th:insert="publicHeader"/>
<script th:src="@{/publicHeader.js}"></script>



<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">


    <div class="container">
        <div class="row" >
            <div class="col-md-8">
                <select class="form-control" name="address" v-model="defaultAddressId">

                    <option v-for="a in addressList" :value="a.id" >{{a.address}}   {{a.tel}}   {{a.name}}</option>
                </select>
            </div>
            <div class="col-md-4">
                <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" @click="clearAddForm">新增地址</button>
            </div>
        </div>
    </div>
    <br/>

    <div class="row">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <table class="table">
                        <tr>
                            <th>商品名</th>
                            <th>商品图片</th>
                            <th>商品单价(元)</th>
                            <th>购买数量</th>
                            <th>小计</th>
                        </tr>
                        <tr v-for="cartItem in cartItems">
                            <td v-text="cartItem.productName" style="height:60px;line-height: 60px">商品名</td>
                            <td>
                                <img :src="cartItem.productImgsrc" style="width: 60px;height: 60px; border-radius: 50%"/>
                            </td>
                            <td v-text="cartItem.productPrice" style="height:60px;line-height: 60px">商品单价</td>
                            <td v-text="cartItem.buyCount" style="height:60px;line-height: 60px">购买数量</td>
                            <td v-text="cartItem.sumPrice" style="height:60px;line-height: 60px">商品小计</td>
                        </tr>
                    </table>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6">
                    <b> 总价(元): {{totalPrice}} </b>
                </div>

                <div class="col-md-6">
                    <button class="btn btn-success" @click.prevent="submitOrder">提交订单</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新增地址</h4>
                </div>
                <div class="modal-body">
                    <form>

                        <div class="form-group">
                            <label for="address">详细地址</label>
                            <input type="text" v-model="address" class="form-control" id="address" placeholder="请输入详细地址">
                        </div>

                        <div class="form-group">
                            <label for="tel">手机号</label>
                            <input type="text" v-model="tel" class="form-control" id="tel" placeholder="请输入手机号">
                        </div>

                        <div class="form-group">
                            <label for="receiver">收件人姓名</label>
                            <input type="text" v-model="receiver" class="form-control" id="receiver" placeholder="请输入收件人">
                        </div>

                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" @click.prevent="saveAddress">保存</button>
                </div>
            </div>
        </div>
    </div>



</div>

<script>

    console.log(".........orderPreview...........")

    new Vue({
        el: "#app"
        ,
        data: {
            productIds:[[${productIds}]],
            totalPrice:"0.00",
            cartItems:[],
            addressList:[],
            defaultAddressId:-1,
            address:'',
            tel:'',
            receiver:''
        }
        ,
        methods: {
            //根据productIds读取要结算的项目
            //向后端发送请求,获取要结算项目的详情,在页面上显示

            initCartOrder(){
                let _this=this;

                //根据productIds 获取结算页详情
                $.post("/order/allItems",{productIds: this.productIds},function (data) {
                    console.log(data)

                    _this.totalPrice = data.totalPrice;
                    _this.cartItems = data.cartItems;

                })


            }
            ,
            initAddressList(){
                //去后端获取当前用户的所有收货地址
                let _this = this;

                $.get("/address/all",function (data) {
                    console.log(data)

                    _this.addressList = data;


                })

            }
            ,
            saveAddress(){

                //搜集表单数据

                let params = {
                    address:this.address,
                    tel:this.tel,
                    name:this.receiver
                }

                console.log(params)

                let _this=this;

                //数据提交后端接口,新增地址
                $.post("/address/add",params,function () {

                    //关闭弹窗
                    $("#myModal").modal('hide')

                    //刷新可选地址
                    _this.initAddressList();
                })


            }
            ,
            clearAddForm(){
                //打开新增地址表单时,清空表单数据
                this.address="";
                this.tel="";
                this.receiver=""
            }
            ,
            submitOrder(){
                //提交订单:  要结算的商品的id    收货地址id   当前用户信息(后台直接获取)
                console.log(this.productIds)

                let params={
                    productIds : this.productIds,
                    addressId : this.defaultAddressId
                }

                console.log(params)

                $.post("/order/createOrder",params,function (data) {
                    console.log(data)

                    if(data == "ok"){
                        //订单生成成功,前往我的订单页
                        console.log("订单生成成功")

                        location.href="/myOrder.html";

                    }else{
                        alert(data)
                    }
                })


            }

        }
        ,
        created(){
            this.initCartOrder();
            this.initAddressList();
        }

    });


</script>


<div th:insert="publicFooter"/>
<script th:src="@{/publicFooter.js}"></script>


</body>
</html>